package cn.tedu.egweibo7.controller;


import cn.tedu.egweibo7.base.response.JsonResult;
import cn.tedu.egweibo7.mapper.CommentMapper;
import cn.tedu.egweibo7.pojo.dto.CommentDTO;
import cn.tedu.egweibo7.pojo.entity.Comment;
import cn.tedu.egweibo7.pojo.vo.CommentListVO;
import cn.tedu.egweibo7.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@Api(tags = "03.评论模块")
@RequestMapping("/v1/comment/")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation(value = "发布评论")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody @Validated CommentDTO commentDTO,
                             @ApiIgnore HttpSession httpSession){
        log.debug("commentDTO={}",commentDTO);
        UserVO userVO = (UserVO) httpSession.getAttribute("user");
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insert(comment);

        return JsonResult.ok();
    }

    @ApiOperation(value = "获取评论列表")
    @GetMapping("selectByWeiboId")
    @ApiImplicitParam(name = "id",value = "微博编号",required = true,dataType = "long")
    public JsonResult selectByWeiboId(@Range(min = 200,max = 300,message = "微博ID必须在200~300之间")
                                      @RequestParam Long id){
        log.debug("id={}",id);
        List<CommentListVO> commentListVOS =  commentMapper.selectByWeiboId(id);
        return JsonResult.ok(commentListVOS);

    }

}
